﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Data;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Util;
using NPOI.SS.UserModel;


namespace DBA.Common
{
    /// <summary>
    /// Excel的MODEL
    /// </summary>
    public class ExcelModel
    {
        /// <summary>
        /// 字段英文名
        /// </summary>
        public string FieldEnglishName
        {
            get;
            set;
        }

        /// <summary>
        /// 字段中文名
        /// </summary>
        public string FieldChineseName
        {
            get;
            set;
        }

    }

    public class NPOIHelper
    {
        #region 暂时屏蔽代码
        /*--
        public static MemoryStream SchoolStateExport(DataTable dt)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();

            //Create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "ZongHenLiuHe";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //Create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "SchoolInfo";
            hssfworkbook.SummaryInformation = si;

            Sheet sheet = hssfworkbook.CreateSheet("sheet");
            Row row = sheet.CreateRow(0);
            Cell cell = null;

            CellStyle style = hssfworkbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.CENTER;
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            Font font = hssfworkbook.CreateFont();
            font.FontHeight = 15 * 15;
            font.Boldweight = 600;
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            style.SetFont(font);
            style.WrapText = true;
            style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
            style.FillPattern = FillPatternType.SOLID_FOREGROUND;


            sheet.SetColumnWidth(0, 390 * 256 / 7);
            sheet.SetColumnWidth(1, 140 * 256 / 7);
            sheet.SetColumnWidth(2, 115 * 256 / 7);
            sheet.SetColumnWidth(3, 115 * 256 / 7);


            for (int n = 0; n < 4; n++)
            {
                cell = row.CreateCell(n);
                cell.CellStyle = style;
            }

            //merged cells on single row
            //ATTENTION: don't use Region class, which is obsolete

            row.Height = 15 * 15 * 2;
            cell = row.GetCell(0);
            row.Height = 15 * 15 * 2;

            row = sheet.GetRow(0);
            cell.SetCellValue("学校名称");
            cell.CellStyle = style;

            cell = row.GetCell(1);
            cell.SetCellValue("学校编号");
            cell.CellStyle = style;


            cell = row.GetCell(2);
            cell.SetCellValue("用户名称");
            cell.CellStyle = style;


            cell = row.GetCell(3);
            cell.SetCellValue("电话号码");
            cell.CellStyle = style;



            int i = 0;
            style = hssfworkbook.CreateCellStyle();
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            font = hssfworkbook.CreateFont();
            font.FontHeight = 14 * 15;
            style.SetFont(font);

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            foreach (DataRow dataRow in dt.Rows)
            {
                ++i;
                row = sheet.CreateRow(i);
                row.Height = 14 * 14 * 2;
                for (int j = 0; j < 4; j++)
                {
                    cell = row.CreateCell(j);
                    cell.CellStyle = style;
                    cell.SetCellValue(dataRow[j].ToString());

                }
            }





            using (MemoryStream ms = new MemoryStream())
            {
                hssfworkbook.Write(ms);
                ms.Flush();
                ms.Position = 0;

                sheet.Dispose();
                hssfworkbook.Dispose();

                return ms;
            }

        }


        public static MemoryStream Export(DataTable dt)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();

            //Create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "ZongHenLiuHe";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //Create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "SchoolInfo";
            hssfworkbook.SummaryInformation = si;

            Sheet sheet = hssfworkbook.CreateSheet("sheet");

            sheet.SetColumnWidth(0, 390 * 256 / 7);
            sheet.SetColumnWidth(1, 140 * 256 / 7);
            sheet.SetColumnWidth(2, 115 * 256 / 7);
            sheet.SetColumnWidth(3, 115 * 256 / 7);
            sheet.SetColumnWidth(4, 115 * 256 / 7);
            sheet.SetColumnWidth(5, 115 * 256 / 7);
            sheet.SetColumnWidth(6, 115 * 256 / 7);
            sheet.SetColumnWidth(7, 120 * 256 / 7);


            Row row = sheet.CreateRow(0);
            //Row row_01 = sheet.CreateRow(1);
            //Row row_02 = sheet.CreateRow(2);
            //row.HeightInPoints = 30;


            Cell cell = null;
            //set the title of the sheet


            CellStyle style = hssfworkbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.CENTER;
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            Font font = hssfworkbook.CreateFont();
            font.FontHeight = 15 * 15;
            font.Boldweight = 600;
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            style.SetFont(font);
            style.WrapText = true;
            style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
            style.FillPattern = FillPatternType.SOLID_FOREGROUND;


            int m = 0;
            int n = 3;
            for (m = 0; m < 3; m++)
            {
                row = sheet.CreateRow(m);
                for (n = 0; n < 8; n++)
                {
                    cell = row.CreateCell(n);
                    cell.CellStyle = style;
                }
            }

            //merged cells on single row
            //ATTENTION: don't use Region class, which is obsolete
            row = sheet.GetRow(0);
            row.Height = 15 * 15 * 2;
            cell = row.GetCell(0);
            cell.SetCellValue("学校名称");
            sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 0));
            cell.CellStyle = style;

            cell = row.GetCell(1);
            cell.SetCellValue("学校简介录入字数");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(0, 2, 1, 1));

            cell = row.GetCell(2);
            cell.SetCellValue("基础信息录入情况");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 2, 3));

            cell = row.GetCell(4);
            cell.SetCellValue("媒体数据录入情况");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 4, 7));


            row = sheet.GetRow(1);
            row.Height = 15 * 15 * 2;

            cell = row.GetCell(2);
            cell.SetCellValue("指标完成数");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(1, 2, 2, 2));

            cell = row.GetCell(3);
            cell.SetCellValue("指标未完成数");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(1, 2, 3, 3));


            cell = row.GetCell(4);
            cell.SetCellValue("音视频录入数");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(1, 2, 4, 4));


            cell = row.GetCell(5);
            cell.SetCellValue("风貌图片录入数");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(1, 1, 5, 6));

            cell = row.GetCell(7);
            cell.SetCellValue("主题图片录入数");
            cell.CellStyle = style;
            sheet.AddMergedRegion(new CellRangeAddress(1, 2, 7, 7));


            row = sheet.GetRow(2);
            row.Height = 15 * 15 * 2;
            cell = row.GetCell(5);
            cell.SetCellValue("旧风貌录入数");
            cell.CellStyle = style;


            cell = row.GetCell(6);
            cell.SetCellValue("新风貌录入数");
            cell.CellStyle = style;

            int i = 2;
            style = hssfworkbook.CreateCellStyle();
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            font = hssfworkbook.CreateFont();
            font.FontHeight = 14 * 15;
            style.SetFont(font);

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            foreach (DataRow dataRow in dt.Rows)
            {
                ++i;
                row = sheet.CreateRow(i);
                row.Height = 14 * 14 * 2;
                for (int j = 0; j < 8; j++)
                {
                    cell = row.CreateCell(j);
                    cell.CellStyle = style;
                    cell.SetCellValue(dataRow[j].ToString());

                }
            }





            using (MemoryStream ms = new MemoryStream())
            {
                hssfworkbook.Write(ms);
                ms.Flush();
                ms.Position = 0;

                sheet.Dispose();
                hssfworkbook.Dispose();

                return ms;
            }

        }

        /// <summary>
        /// DataTable导出到Excel的MemoryStream
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <returns></returns>
        public static MemoryStream Export(DataTable dtSource, string strHeaderText)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            Sheet sheet = workbook.CreateSheet();

            #region 右击文件 属性信息
            {
                //Create a entry of DocumentSummaryInformation
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "ZhongHen";
                workbook.DocumentSummaryInformation = dsi;

                //Create a entry of SummaryInformation
                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                si.Subject = "Excel Exprot";
                workbook.SummaryInformation = si;
            }
            #endregion

            CellStyle dateStyle = workbook.CreateCellStyle();
            DataFormat format = workbook.CreateDataFormat();
            dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

            //取得列宽
            int[] arrColWidth = new int[dtSource.Columns.Count];
            foreach (DataColumn item in dtSource.Columns)
            {
                arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
            }
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
                    if (intTemp > arrColWidth[j])
                    {
                        arrColWidth[j] = intTemp;
                    }
                }
            }



            int rowIndex = 0;

            foreach (DataRow row in dtSource.Rows)
            {
                #region 新建表，填充表头，填充列头，样式
                if (rowIndex == 65535 || rowIndex == 0)
                {
                    if (rowIndex != 0)
                    {
                        sheet = workbook.CreateSheet();
                    }

                    #region 表头及样式
                    {
                        Row headerRow = sheet.CreateRow(0);
                        headerRow.HeightInPoints = 25;
                        headerRow.CreateCell(0).SetCellValue(strHeaderText);

                        CellStyle headStyle = workbook.CreateCellStyle();
                        headStyle.Alignment = HorizontalAlignment.CENTER;
                        Font font = workbook.CreateFont();
                        font.FontHeightInPoints = 20;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);
                        headStyle.BorderTop = CellBorderType.THIN;

                        headStyle.TopBorderColor = 0x0e;

                        headerRow.GetCell(0).CellStyle = headStyle;

                        sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1));

                    }
                    #endregion


                    #region 列头及样式
                    {
                        Row headerRow = sheet.CreateRow(1);


                        CellStyle headStyle = workbook.CreateCellStyle();
                        headStyle.Alignment = HorizontalAlignment.CENTER;
                        Font font = workbook.CreateFont();
                        font.FontHeightInPoints = 10;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);


                        foreach (DataColumn column in dtSource.Columns)
                        {
                            headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                            headerRow.GetCell(column.Ordinal).CellStyle = headStyle;

                            //设置列宽
                            sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);

                        }

                    }
                    #endregion

                    rowIndex = 2;
                }
                #endregion


                #region 填充内容
                foreach (DataColumn column in dtSource.Columns)
                {
                    Row dataRow = sheet.CreateRow(rowIndex);
                    Cell newCell = dataRow.CreateCell(column.Ordinal);

                    string drValue = row[column].ToString();
                    newCell.SetCellValue(drValue);

                    //switch (column.DataType.ToString())
                    //{
                    //    case "System.String"://字符串类型
                    //        newCell.SetCellValue(drValue);
                    //        break;
                    //    case "System.DateTime"://日期类型
                    //        DateTime dateV;
                    //        DateTime.TryParse(drValue, out dateV);
                    //        newCell.SetCellValue(dateV);

                    //        newCell.CellStyle = dateStyle;//格式化显示
                    //        break;
                    //    case "System.Boolean"://布尔型
                    //        bool boolV = false;
                    //        bool.TryParse(drValue, out boolV);
                    //        newCell.SetCellValue(boolV);
                    //        break;
                    //    case "System.Int16"://整型
                    //    case "System.Int32":
                    //    case "System.Int64":
                    //    case "System.Byte":
                    //        int intV = 0;
                    //        int.TryParse(drValue, out intV);
                    //        newCell.SetCellValue(intV);
                    //        break;
                    //    case "System.Decimal"://浮点型
                    //    case "System.Double":
                    //        double doubV = 0;
                    //        double.TryParse(drValue, out doubV);
                    //        newCell.SetCellValue(doubV);
                    //        break;
                    //    case "System.DBNull"://空值处理
                    //        newCell.SetCellValue("");
                    //        break;
                    //    default:
                    //        newCell.SetCellValue("");
                    //        break;
                    //}

                }
                #endregion

                rowIndex++;
            }


            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;

                sheet.Dispose();
                workbook.Dispose();

                return ms;
            }



        }


        /// <summary>
        /// DataTable导出到Excel文件
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <param name="strFileName">保存位置</param>
        public static void Export(DataTable dtSource, string strHeaderText, string strFileName)
        {
            using (MemoryStream ms = Export(dtSource, strHeaderText))
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    byte[] data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                }
            }
        }

        /// <summary>
        /// 用于Web导出
        /// </summary>
        /// <param name="dtSource"></param>
        /// <param name="strHeaderText"></param>
        /// <param name="strFileName"></param>
        public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
        {

            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
            curContext.Response.End();

        }


        public static void ExportByWeb(DataTable dtSource, string strFileName)
        {

            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            curContext.Response.BinaryWrite(Export(dtSource).GetBuffer());
            curContext.Response.End();

        }


        public static void SchoolStateExportByWeb(DataTable dt, string strFileName)
        {
            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            curContext.Response.BinaryWrite(SchoolStateExport(dt).GetBuffer());
            curContext.Response.End();
        }
         * --*/
        #endregion



        public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
        {

            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            curContext.Response.BinaryWrite(CreateMemoryStream(dtSource,strHeaderText).GetBuffer());
            curContext.Response.End();

        }

        public static void ExportByWeb(DataTable dtSource, string templatePath, int rowStart, string strFileName)
        {

            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            curContext.Response.BinaryWrite(CreateMemoryStream(dtSource, templatePath, rowStart).GetBuffer());
            curContext.Response.End();

        }

        public static DataTable ImportDataTable(string strFileName)
        {
            DataTable dt = new DataTable();
            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            Sheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            Row headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;

            for (int j = 0; j < cellCount; j++)
            {
                Cell cell = headerRow.GetCell(j);
                dt.Columns.Add(cell.ToString());
            }

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                Row row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();

                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                dt.Rows.Add(dataRow);
            }
            return dt;
        }


        public static MemoryStream CreateMemoryStream(DataTable dt,string headerStrs)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();

            //Create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "ZongHenLiuHe";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //Create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "SchoolInfo";
            hssfworkbook.SummaryInformation = si;

            Sheet sheet = hssfworkbook.CreateSheet("sheet");
            Row row = sheet.CreateRow(0);
            Cell cell = null;

            CellStyle style = hssfworkbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.CENTER;
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            Font font = hssfworkbook.CreateFont();
            font.FontHeight = 15 * 15;
            font.Boldweight = 600;
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            style.SetFont(font);
            style.WrapText = true;
            style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
            style.FillPattern = FillPatternType.SOLID_FOREGROUND;

            int rowsCount = dt.Rows.Count;
            int cellCount = dt.Columns.Count;

            for (int i = 0; i < cellCount; i++)
            {
                sheet.SetColumnWidth(i, 140 * 256 / 7);
            }

            for (int n = 0; n < cellCount; n++)
            {
                cell = row.CreateCell(n);
                cell.CellStyle = style;
            }

            //merged cells on single row
            //ATTENTION: don't use Region class, which is obsolete

            row = sheet.GetRow(0);
            row.Height = 15 * 15 * 2;
            if (VerificationHelper.IsNullOrEmpty<string>(headerStrs))
            {
                for (int j = 0; j < cellCount; j++)
                {
                    cell = row.GetCell(j);
                    cell.SetCellValue(dt.Columns[j].ColumnName);
                    cell.CellStyle = style;
                }
            }
            else
            {
                string[] columnNameArray = headerStrs.Split(',');
                for (int j = 0; j < columnNameArray.Length; j++)
                {
                    cell = row.GetCell(j);
                    cell.SetCellValue(columnNameArray[j]);
                    cell.CellStyle = style;
                }
            }

            int setRowCount = 1;
            style = hssfworkbook.CreateCellStyle();
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            font = hssfworkbook.CreateFont();
            font.FontHeight = 14 * 15;
            style.SetFont(font);

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            foreach (DataRow dataRow in dt.Rows)
            {
                row = sheet.CreateRow(setRowCount);
                ++setRowCount;
                row.Height = 14 * 14 * 2;
                for (int j = 0; j < cellCount; j++)
                {
                    cell = row.CreateCell(j);
                    cell.CellStyle = style;
                    cell.SetCellValue(dataRow[j].ToString());

                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                hssfworkbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                sheet.Dispose();
                hssfworkbook.Dispose();
                return ms;
            }

        }


        public static MemoryStream CreateMemoryStream(DataTable dt, string templatePath, int rowStart)
        {
            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(templatePath, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }

            //Create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "ZongHenLiuHe";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //Create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "SchoolInfo";
            hssfworkbook.SummaryInformation = si;

            Sheet sheet = hssfworkbook.GetSheetAt(0);

            //Row row = sheet.CreateRow(rowStart);
            CellStyle style = hssfworkbook.CreateCellStyle();
            Font font = hssfworkbook.CreateFont();

            int setRowCount = 1;
            style = hssfworkbook.CreateCellStyle();
            style.VerticalAlignment = VerticalAlignment.CENTER;
            //create a font style
            font = hssfworkbook.CreateFont();
            font.FontHeight = 14 * 15;
            style.SetFont(font);

            style.BorderTop = CellBorderType.THIN;
            style.BorderLeft = CellBorderType.THIN;
            style.BorderRight = CellBorderType.THIN;
            style.BorderBottom = CellBorderType.THIN;

            style.TopBorderColor = 0x00;
            style.LeftBorderColor = 0x00;
            style.RightBorderColor = 0x00;
            style.BottomBorderColor = 0x00;

            foreach (DataRow dataRow in dt.Rows)
            {
                Row row = sheet.CreateRow(rowStart + setRowCount);
                ++setRowCount;
                row.Height = 14 * 14 * 2;
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Cell cell = row.CreateCell(j);
                    cell.CellStyle = style;
                    cell.SetCellValue(dataRow[j].ToString());

                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                hssfworkbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                sheet.Dispose();
                hssfworkbook.Dispose();
                return ms;
            }

        }


    }


}
